#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogcreate.h"

#include <QDebug>

#include <iostream>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_bOpen(false)
{
    ui->setupUi(this);
    m_dbMgr = make_shared<DBMgr>();
    m_fileMgr = make_shared<FileMgr>(m_dbMgr);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionCreate_triggered()
{
    shared_ptr<DialogCreate> dc = make_shared<DialogCreate>(this);
    dc->exec();
    if (dc->isValid())
    {
        m_bOpen = true;
        m_projectName = dc->projectName();
        m_projectPath = dc->projectPath();
        m_sourcePath = dc->sourcePath();
        m_supportedSuffixes = dc->supportedSuffixes();
        createNewProject();
    }
}

void MainWindow::createNewProject()
{
    m_dbMgr->createDB(m_projectPath + "/" + m_projectName + ".pdep");
    for (string suffix : *m_supportedSuffixes)
    {
        m_dbMgr->insertType(suffix);
    }
    m_fileMgr->setSupportedTypes(m_dbMgr->queryAllType());
    m_fileMgr->traverseFile(m_sourcePath);
    statusBar()->setStatusTip("Project created");
}
